//
//  BAAlbumDetailVC.m
//  BabyAlbum
//
//  Created by master on 17/2/15.
//  Copyright © 2017年 master. All rights reserved.
//

#import "BAAlbumDetailVC.h"
#import "BADetailCellNode.h"
#import "BAAlbumIndexManager.h"
@interface BAAlbumDetailVC () <ASPagerDelegate,ASPagerDataSource>

//@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, strong) ASPagerNode *pagerNode;
@property (nonatomic, assign) BOOL alreadySet;
//@property (nonatomic, assign) BOOL isEditing;
@property (nonatomic, strong) UIButton *editButton;
@property (nonatomic, strong) BADetailCellNode *currentCellNode;
@property (nonatomic, strong) NSArray *albums;
@property (nonatomic, assign) BOOL isNew;

@end

@implementation BAAlbumDetailVC


- (instancetype)initWithNewAlbum {
    self.isNew = YES;
    return [self init];
}

- (instancetype)init {
    ASPagerFlowLayout *layout = [[ASPagerFlowLayout alloc] init];
//    layout.itemSize = CGSizeMake(kScreenWidth - 10, kScreenHeight);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(134, 0, 0, 0);
    self.pagerNode = [[ASPagerNode alloc] initWithCollectionViewLayout:layout];
    self.pagerNode.delegate = self;
    self.pagerNode.dataSource = self;
    self.pagerNode.view.bounces = YES;
     self.pagerNode.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

//    self.pagerNode.view.contentInset = UIEdgeInsetsMake(104, 8, 0, 8);
//    self.collectionNode.view.contentInset = UIEdgeInsetsMake(44, 0, 0, 0);

    if(self = [super initWithNode:self.pagerNode]) {
        if (_isNew) {
            BAAlbumModel *newAlbum = [[BAAlbumModel alloc]init];
            newAlbum.custom_date = BATimeStamp;
            _albums = @[newAlbum];

        } else {
            _albums = BAALBUM_DATA.albumList;
        }
//        [self.node addSubnode:self.pagerNode];
        self.node.backgroundColor = RGB_255(235, 240, 239);
    }
    return self;
}

//- (void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
////    self.navigationController.navigationBarHidden = YES;
//}

//- (void)viewWillDisappear:(BOOL)animated {
//    [super viewDidAppear:animated];
////    self.navigationController.navigationBarHidden = NO;
//}



- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    [self.pagerNode relayoutItems];

    if(BAALBUM_DATA.currentIndex * kScreenWidth < self.pagerNode.view.contentSize.width && !_alreadySet) {
        self.pagerNode.view.contentOffset = CGPointMake(BAALBUM_DATA.currentIndex * kScreenWidth, self.pagerNode.view.contentOffset.y);
        _alreadySet = YES;
    }
}


- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSArray<BADetailCellNode *> *nodeList = [self.pagerNode visibleNodes];
    if(nodeList.firstObject != self.currentCellNode) {
        self.currentCellNode = nodeList.firstObject;
        [self resetNaviAndData];
    }
}

- (void)initNavi {
    self.editButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 25)];
    self.editButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:self.editButton];


    self.navigationItem.rightBarButtonItem = rightBarButton;
    self.editButton.titleLabel.textAlignment = NSTextAlignmentRight;
    [self.editButton addTarget:self action:@selector(setEditStatus) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)setEditStatus {
    if(self.currentCellNode.isEditing) {
        [MBProgressHUD showHUDAddedTo:self.node.view animated:YES];

        BOOL saved = [self.currentCellNode save];
        [MBProgressHUD hideHUDForView:self.node.view];

        if(!saved) {
            return;
        }
        if(self.isNew) {
            BAALBUM_DATA.currentIndex = BAALBUM_DATA.albumList.count - 1;

        }
        self.currentCellNode.isEditing = !self.currentCellNode.isEditing;
        [self resetNaviAndData];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNavi];
    [self resetNaviAndData];

    self.pagerNode.view.contentSize = CGSizeMake(BAALBUM_DATA.albumList.count * kScreenWidth, kScreenHeight);

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)resetNaviAndData {
//    if(self.isNew) {
//        self.currentCellNode.isEditing = YES;
//    }else {
//        self.currentCellNode.isEditing = NO;
//    }
    
    if(self.isNew) {
        self.title = @"添加卡片";
    } else {
        self.title = self.currentCellNode.albumModel.title;
    }
    if(self.currentCellNode) {
        if(self.currentCellNode.isEditing) {
            [self.editButton setImage:[UIImage imageNamed:@"edit_complete"] forState:UIControlStateNormal];
            [self.editButton setTitle:@"" forState:UIControlStateNormal];
            
        } else {
            [self.editButton setImage:nil forState:UIControlStateNormal];
            [self.editButton setTitle:@"分享" forState:UIControlStateNormal];
            [self.editButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
    }
    
}


#pragma mark ---  ASPagerDataSource

- (NSInteger)numberOfPagesInPagerNode:(ASPagerNode *)pagerNode {
    return _albums.count;
}

- (ASCellNodeBlock)pagerNode:(ASPagerNode *)pagerNode nodeBlockAtIndex:(NSInteger)index {
    BAAlbumModel *model = _albums[index];
    __weak typeof(self) weakSelf = self;
    return ^{
        BADetailCellNode *cellNode = [[BADetailCellNode alloc] initWithData:model editing:weakSelf.isNew];
        cellNode.manager = self;
        [cellNode setEditStatusBlock:^(BOOL editing) {
            [weakSelf resetNaviAndData];
        }];
        return cellNode;
    };
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if(_isNew ) {
        return;
    }
    [self.currentCellNode.view endEditing:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if(_isNew ) {
        return;
    }
//    NSArray<BADetailCellNode *> *nodeList = [self.pagerNode visibleNodes];
//    if(nodeList.count) {
        BAALBUM_DATA.currentIndex = self.pagerNode.currentPageIndex;
        self.currentCellNode = (BADetailCellNode *)[self.pagerNode nodeForPageAtIndex:self.pagerNode.currentPageIndex];
        [self resetNaviAndData];
}

#pragma - present的时候需要的代理方法
#pragma mark - UIViewControllerTransitioningDelegate
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    return [MKJPresentAnimator new];
}


- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    return [MKJPresentAnimator new];
}

- (void)dealloc {
    NSLog(@"dealloc");
}

@end
